/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.URL;
import java.util.Arrays;
import javax.imageio.ImageIO;
import models.*;

/**
 *
 * @author dennis
 */
public class GamePanel extends javax.swing.JPanel{
    
    private Tile[][] matrix = new Tile[0][0];
    private boolean isFinished = false;
    
    public GamePanel(int width, int height){
        
        setLayout( null );
        
        setBackground( java.awt.Color.white );
        setPreferredSize( new Dimension(width, height));        
        
        setFocusable(true);
        requestFocusInWindow();
        
    }
    
    public GamePanel(Tile[][] matrix, int width, int height){
        
        this(width, height);
        
        this.matrix = matrix;
         
    }
     
    public void noLevelMessage(){
    }
    
    public void paintTiles(Graphics g){

        URL loc = this.getClass().getResource("../images/bg.jpg");
        ImageIcon iia = new ImageIcon(loc);
        Image image = iia.getImage();
        g.drawImage(image,0, 0, this);

        if(isFinished){
            URL welc = this.getClass().getResource("../images/finished.png");
            ImageIcon welci = new ImageIcon(welc);
            Image welcome = welci.getImage();
            g.drawImage(welcome, 0, 0, this);
        } else {
            
           if(this.matrix.length > 0){
                for(int i = 0; i < this.matrix.length; i++){
                    for(int j = 0; j < this.matrix[i].length; j++){

                        if(matrix[i][j] instanceof WallTile){
                            g.drawImage(((WallTile) matrix[i][j]).getGraphic(), matrix[i][j].getOffsetX() + 100, matrix[i][j].getOffsetY() + 100, this);
                        } else if(matrix[i][j] instanceof FinishTile){
                            g.drawImage(((FinishTile) matrix[i][j]).getGraphic(), matrix[i][j].getOffsetX() + 100, matrix[i][j].getOffsetY() + 100, this);
                        } else if(matrix[i][j] instanceof RoadTile){
                            g.drawImage(((RoadTile) matrix[i][j]).getGraphic(), matrix[i][j].getOffsetX() + 100, matrix[i][j].getOffsetY() + 100, this);
                        }

                    }
                }
            } else {
                URL welc = this.getClass().getResource("../images/welcome.png");
                ImageIcon welci = new ImageIcon(welc);
                Image welcome = welci.getImage();
                g.drawImage(welcome, 0, 0, this);

            } 
        }
    }
    
    public void setMatrix(Tile[][] matrix){
        this.matrix = matrix;
        //System.out.println(Arrays.toString(matrix));
    }
    
    public void setFinished(){
        isFinished = true;
    }
    
    @Override
    public void paint(Graphics g){
        paintTiles(g);
    }    
    
}
